package com.yuanqi.controller;

import com.yuanqi.domain.ManualPayment;
import com.yuanqi.service.ManualPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/manual-payments")
@CrossOrigin(origins = "*")
public class ManualPaymentController {
    
    @Autowired
    private ManualPaymentService manualPaymentService;
    
    @GetMapping
    public ResponseEntity<Map<String, Object>> getManualPayments(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String supplier,
            @RequestParam(required = false) String paymentStatus,
            @RequestParam(required = false) String paymentDate) {
        
        try {
            List<ManualPayment> payments = manualPaymentService.findAll();
            
            Map<String, Object> response = new HashMap<>();
            response.put("content", payments);
            response.put("totalElements", payments.size());
            response.put("totalPages", 1);
            response.put("currentPage", page);
            response.put("size", size);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PostMapping
    public ResponseEntity<Map<String, Object>> createManualPayment(@RequestBody ManualPayment paymentRequest) {
        try {
            ManualPayment createdPayment = manualPaymentService.createPayment(paymentRequest);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "手动付款创建成功");
            response.put("paymentId", createdPayment.getId());
            response.put("paymentCode", createdPayment.getPaymentCode());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateManualPayment(@PathVariable Long id, @RequestBody ManualPayment paymentRequest) {
        try {
            paymentRequest.setId(id);
            ManualPayment updatedPayment = manualPaymentService.updatePayment(paymentRequest);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "手动付款更新成功");
            response.put("paymentId", updatedPayment.getId());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteManualPayment(@PathVariable Long id) {
        try {
            manualPaymentService.deletePayment(id);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "手动付款删除成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getManualPaymentStats() {
        try {
            Map<String, Object> stats = manualPaymentService.getStatistics();
            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
}


